package com.zdp.middleware.whitelist.config;

import com.zdp.middleware.whitelist.DoJoinPoint;
import org.apache.commons.logging.Log;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 白名单 自动配置类，由 spring.factories 自动引入
 * 这个Spring.factories 是Spring 的 SPI机制吧
 * @author ZhengDp
 * @Date 2023/5/26 18:41
 */
@Configuration
// Class位于类路径上，才会加载该 配置类对象到Spring容器中
@ConditionalOnClass(WhiteListProperties.class)
// 使WhiteListProperties类上的 @ConfigurationProperties 生效，并将该类注入到IOC容器中
@EnableConfigurationProperties(WhiteListProperties.class)
public class WhiteListAutoConfigure {

    private Logger logger = LoggerFactory.getLogger(WhiteListProperties.class);

    /*
    * 返回白名单用户
    * */
    @Bean("whiteListConfig")
    // 用户可以硬编码 白名单用户，也可以在配置文件中 填写白名单用户
    @ConditionalOnMissingBean
    public String whiteListConfig(WhiteListProperties properties) {
        logger.info("White 自动装配");
        return properties.getUsers();
    }

    @Bean("WhiteJoinPoint")
    public DoJoinPoint doJoinPoint(){
        return new DoJoinPoint();
    }
}
